package com.gykfbxxh.imovie.view.user.activity;

import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.utils.util.LogUtils;
import com.google.gson.reflect.TypeToken;
import com.gykfbxxh.imovie.AppApplication;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.adapter.FilmsListAdapter;
import com.gykfbxxh.imovie.base.BaseListActivity;
import com.gykfbxxh.imovie.bean.film.FilmListBean;
import com.gykfbxxh.imovie.bean.film.FilmListEntity;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.listener.HttpListener;
import com.gykfbxxh.imovie.utils.HttpUtils;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;

import java.util.ArrayList;
import java.util.List;

import static com.gykfbxxh.imovie.adapter.FilmsListAdapter.NOTICE_VIEW;

public class NoticeActivity extends BaseListActivity<FilmListEntity> {

    private int mMark;//下拉刷新请求标识
    @Override
    protected void changeView() {
        mPageName = "NoticeActivity" ;
        setTitleShow(true);
        setTitle("我的消息");
    }

    @Override
    protected void changeData(final boolean isRefresh) {

        HttpUtils.addGetRequest(URIContant.GETUSERMESSAGELIST+dataPage, new TypeToken<FilmListBean>() {
        }.getType(), mPageName, new HttpListener<FilmListBean>() {

            @Override
            public void onStart() {
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(FilmListBean response) {
                LogUtils.e("xuxh", "onSuccess");
                if (response != null && response.getResult() != null) {
                    FilmListBean.ResultBean result = response.getResult();
                    if (result != null) {
                        List<FilmListEntity> list = result.getList();
                        if (list != null && list.size() > 0) {
                            if (dataList == null) {
                                dataList = new ArrayList<FilmListEntity>();
                            }
                            if (isRefresh) {
                                dataList.clear();
                            }
                            dataList.addAll(list);
                            if (mAdapter == null) {
                                mAdapter = new FilmsListAdapter(dataList, mActivity, NOTICE_VIEW);
                                mListView.setAdapter(mAdapter);
                            } else {
                                mAdapter.notifyDataSetChanged();
                            }
                            if (isRefresh) {
                                if (list.size() > 1) {
                                    String msg = "推荐引擎有<font color='#F9D13F'>" + list.size()  + "</font>条新数据";
                                    setLoadView(LOADSUCES, Html.fromHtml(msg));
                                } else {
                                    //updateLoadView(LOADSUCES, "暂无新数据");
                                    setLoadView(LOADSUCES, getString(R.string.pullup_nomore_data));
                                }
                            } else {
                                setLoadView(LOADSUCES, "");
                            }

                        } else {
                            setLoadView(LOADSUCES, getString(R.string.pullup_nomore_data));
                        }
                    } else {
                        setLoadView(DATAERROR, response.getErrorMsg());
                    }
                } else {
                    setLoadView(DATAERROR, response.getErrorMsg());
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        //防止越界
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        FilmListEntity filmListEntity = dataList.get(position);
        if("0".equals(filmListEntity.getfType())) {
            Intent intent = new Intent(this, FilmDetailActivity.class);
            intent.putExtra(PARAMETER1,filmListEntity.getFObject().getFId());
            startActivity(intent);
        }else if("1".equals(filmListEntity.getfType())){
            String url = filmListEntity.getfUrl();
            chuLiUrl(url);
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse("http://"+url);
            intent.setData(content_url);
            startActivity(intent);
        }
    }

    private void chuLiUrl(String url) {
        url = url.trim();
        if(url.startsWith("http://")||url.startsWith("https://")) {

        }else {
            url = "http://"+ url ;
        }
    }

}
